//
//  MUSICController.m
//  GKBHouse
//
//  Created by 宣洋网络 on 16/1/5.
//
//

#import "MUSICController.h"
#import "GKBNavigationBar.h"
#import "DeviceView.h"
#import "GKBMainController.h"

@interface MUSICController ()


@end

@implementation MUSICController
@synthesize equipment;
@synthesize ctrOrder;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (id)initWithEquipment:(NewEquipment *)equ
{
    self = [super init];
    if (self) {
        // Custom initialization
        self.equipment = equ;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    [self.view setFrame:CGRectMake(0, 0, DEVICE_HEIGHT, DEVICE_WIDTH)];
    [self setUpButton1];
    [self setUpButton2];
    [self setUpButton3];
    [self initTopBar];
    [self initButton];
    
    
    if([self.equipment.state isEqualToString: kStateON])
    {
        [DeviceView setDeivceOnOff:self.equipment button:onOff State:YES];
    }
    else
    {
        [DeviceView setDeivceOnOff:self.equipment button:onOff State:NO];
    }
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
//}
- (void)initTopBar
{
    GKBNavigationBar *topBar = [[GKBNavigationBar alloc]init];
    [topBar setTarget:self returnAction:@selector(closeView) withTitle:self.equipment.name];
    [self.view addSubview:topBar];
    [topBar release];
    
}
- (void)initButton
{
    [onOff addTarget:self action:@selector(ctlDevice) forControlEvents:UIControlEventTouchUpInside];
}
- (void)closeView
{
    [self.view removeFromSuperview];
    [GKBMainController refreshDevices];
    [GKBMainController postStartRefresh];
}
#pragma mark - DVD button

#pragma mark - 电器设备开关状态
- (void)ctlDevice
{
    NSString *switchId = self.equipment.switchId;
    NSString *name = self.equipment.name;
    BOOL STATE = [DeviceView deviceStateOn:self.equipment];
    BOOL OPEN = !STATE;
    [DeviceView OperatSwithcId:switchId name:name OnOff:OPEN target:self end:@selector(ctlDeviceEnd:)];
}

- (void)ctlDeviceEnd:(NSDictionary *)dic
{
    NSLog(@"%@", dic);
    
    NSNumber *result = [dic objectForKey:@"result"];
    
    NSString *resultTip = @"";
    if (result.integerValue == 0) {
        BOOL STATE = [DeviceView deviceStateOn:self.equipment ];
        BOOL OPEN = !STATE;
        //操作成功
        resultTip = [OperationTip operatSuccess:YES type:ErrorTypeServerReturnFail];
        [DeviceView setDeivceOnOff:self.equipment button:onOff State:OPEN];
        GKBMainController *main = [GKBMainController mainCtr];
        [main updateStateByNetTarget:self action:@selector(updateState:)];
    }
    else
    {
        //操作失败！原因：
        resultTip = [OperationTip operatSuccess:NO type:ErrorTypeServerReturnFail];
    }
    [iToast showTip:resultTip gravity:iToastGravityBottom];
}
- (void)updateState:(NSArray*)states
{
    [GKBMainController updateEqu:self.equipment button:onOff states:states];
}
#pragma mark - 控制DVD
- (void)ctrDVD:(NSString *)ORDER
{
    NSString *order = ORDER;
    NSString *orderName = NSLocalizedString(order, nil);
    NSString *operat = NSLocalizedString(@"operat:", nil);
    NSString *startTip = [operat stringByAppendingString:orderName];
    [iToast showTip:startTip gravity:iToastGravityBottom];
    
    [WebServices setShowConnectting:NO];
    
    self.ctrOrder = order;
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    
    //操作失败！原因：
    NSString *errorTip = [OperationTip operatSuccess:NO type:ErrorTypeNetworkFail];
    [WebServices requestStartOrder:@"ctlBackgroundMusic" body:body target:self action:@selector(ctrDVDEnd:) errorTip:errorTip];
}
- (void)ctrDVDEnd:(NSDictionary *)dic
{
    NSLog(@"%@", dic);
    NSNumber *result = [dic objectForKey:@"result"];
    if (result.integerValue == 0) {
        //操作成功
        NSString *tip = [OperationTip operatSuccess:YES type:ErrorTypeServerReturnFail];
        [iToast showTip:tip gravity:iToastGravityBottom];
    }
    else
    {
        //操作失败！原因：
        NSString *tip = [OperationTip operatSuccess:NO type:ErrorTypeServerReturnFail];
        [iToast showTip:tip gravity:iToastGravityBottom];
    }
}

- (void)setUpButton1
{
    NSInteger cols = 6;
    
    CGFloat w;

    if (DEVICE_HEIGHT == 480) {
        w = 370/6;
    }else
    {
        w = 460/6;

    }
    CGFloat h = 40;
    CGFloat marign = 15;
    
    CGFloat x = 0;
    CGFloat y = 0;
    NSInteger col = 0;
    NSInteger row = 0;
    NSArray *backeImageArray = [NSArray arrayWithObjects:@"starandstopsss@3x.png",@"pervss@3x.png",@"nextss@3x.png",@"stopss@3x.png",@"sound@2x.png",@"openss@3x.png",nil];
    for (int i = 0; i <  backeImageArray.count; i ++) {
        col = i % cols;
        row = i / cols;
        x = col * (marign + w);
        y = row * (marign + h);
        UIButton *subjectsBtn = [[UIButton alloc] initWithFrame:CGRectMake(20+x, y+70, w, h)];
        NSString *subjects = [NSString stringWithFormat:@"%@",backeImageArray[i]];
        if (i == backeImageArray.count-2) {
            [subjectsBtn setBackgroundImage:[UIImage imageNamed:@"brownNormal.png"] forState:UIControlStateNormal];
        }
        subjectsBtn.tag = i;
        [subjectsBtn setImage:[UIImage imageNamed:subjects] forState:UIControlStateNormal];
        [subjectsBtn addTarget:self action:@selector(subjectsButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:subjectsBtn];
    }

}

- (void)setUpButton2
{
    NSInteger cols = 6;
    CGFloat w;

    if (DEVICE_HEIGHT == 480) {
        w = 370/6;
    }else
    {
        w = 460/6;
        
    }
    CGFloat h = 40;
    CGFloat marign = 15;
    
    CGFloat x = 0;
    CGFloat y = 0;
    NSInteger col = 0;
    NSInteger row = 0;
    NSArray *backeImageArray = [NSArray arrayWithObjects:@"S+ss@3x.png",@"S-ss@3x.png",@"backss@3x.png",@"ndss@3x.png",@"pdss@3x.png",@"closedss@3x.png",nil];
    for (int i = 0; i <  backeImageArray.count; i ++) {
        col = i % cols;
        row = i / cols;
        x = col * (marign + w);
        y = row * (marign + h);
        UIButton *subjectsBtn = [[UIButton alloc] initWithFrame:CGRectMake(20+x, y+160, w, h)];
        NSString *subjects = [NSString stringWithFormat:@"%@",backeImageArray[i]];
        subjectsBtn.tag = i;
        [subjectsBtn setImage:[UIImage imageNamed:subjects] forState:UIControlStateNormal];
        [subjectsBtn addTarget:self action:@selector(subjectsButton2:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:subjectsBtn];
    }
    
}

- (void)setUpButton3
{
    NSInteger cols = 4;
    CGFloat w;

    if (DEVICE_HEIGHT == 480) {
        w = 310/4;
    }else
    {
        w = 400/4;
    }
    CGFloat h = 40;
    CGFloat marign = 44;
    
    CGFloat x = 0;
    CGFloat y = 0;
    NSInteger col = 0;
    NSInteger row = 0;
    NSArray *backeImageArray = [NSArray arrayWithObjects:@"music@3x.png",@"fmss@3x.png",@"sdss@3x.png",@"auxss@3x.png",nil];
    for (int i = 0; i <  backeImageArray.count; i ++) {
        col = i % cols;
        row = i / cols;
        x = col * (marign + w);
        y = row * (marign + h);
        UIButton *subjectsBtn = [[UIButton alloc] initWithFrame:CGRectMake(20+x, y+250, w, h)];
        NSString *subjects = [NSString stringWithFormat:@"%@",backeImageArray[i]];
        subjectsBtn.tag = i;
        [subjectsBtn setImage:[UIImage imageNamed:subjects] forState:UIControlStateNormal];
        [subjectsBtn setBackgroundImage:[UIImage imageNamed:@"blueNormal.png"] forState:UIControlStateNormal];
        [subjectsBtn addTarget:self action:@selector(subjectsButton3:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:subjectsBtn];
    }
}


- (void)subjectsButton:(UIButton *)subButton
{
    if (subButton.tag == 0) {
        [self ctrDVD:@"PP"];
    }
    else if (subButton.tag == 1) {
        [self ctrDVD:@"PERV"];
    }
    else if (subButton.tag == 2) {
        [self ctrDVD:@"NEXT"];
    }
    else if (subButton.tag == 3) {
        [self ctrDVD:@"STOP"];
    }
    else if (subButton.tag == 4) {
        [self ctrDVD:@"MUTE"];
    }
    else if (subButton.tag == 5) {
        NSString *switchId = self.equipment.switchId;
        NSString *name = self.equipment.name;
        BOOL OPEN = YES;
        [DeviceView OperatSwithcId:switchId name:name OnOff:OPEN target:self end:@selector(ctlDeviceEnd:)];
    }
    
}

- (void)subjectsButton2:(UIButton *)subButton
{
    if (subButton.tag == 0) {
        [self ctrDVD:@"S+"];
    }
    else if (subButton.tag == 1) {
        [self ctrDVD:@"S-"];
    }
    else if (subButton.tag == 2) {
        [self ctrDVD:@"BACK"];
    }
    else if (subButton.tag == 3) {
        [self ctrDVD:@"ND"];
    }
    else if (subButton.tag == 4) {
        [self ctrDVD:@"PD"];
    }
    else if (subButton.tag == 5) {
        NSString *switchId = self.equipment.switchId;
        NSString *name = self.equipment.name;
        BOOL OPEN = NO;
        [DeviceView OperatSwithcId:switchId name:name OnOff:OPEN target:self end:@selector(ctlDeviceEnd:)];
    }
    
}

- (void)subjectsButton3:(UIButton *)subButton
{
    if (subButton.tag == 0) {
        [self ctrDVD:@"USB"];
    }
    else if (subButton.tag == 1) {
        [self ctrDVD:@"FM"];
    }
    else if (subButton.tag == 2) {
        [self ctrDVD:@"SD"];
    }
    else if (subButton.tag == 3) {
        [self ctrDVD:@"AUX"];
    }
    
}

@end
